import { login, logout, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
// import avatarPng from '@/assets/images/avatar.png'

const state = {
  token: getToken(),
  roles: [],
  permissions: [],
  userInfo: {},
  userMsgChange: 0
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_USER_MSG_CHANGE: (state, userMsgChange) => {
    state.userMsgChange = userMsgChange
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, captcha, captchaId, loginType, mac, phone, smsCaptcha } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password, captcha: captcha, captchaId: captchaId, loginType: loginType, mac: mac, phone: phone, smsCaptcha: smsCaptcha }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        console.log('setToken', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      console.log('getUserInfo')
      getUserInfo().then(response => {
        const { data } = response

        if (!data) {
          reject('获取用户信息为空，请联系管理员')
        }
        const { roles, permissions, userInfo } = data

        commit('SET_ROLES', (roles && roles.length > 0) ? roles : ['user'])
        commit('SET_PERMISSIONS', permissions)
        commit('SET_USER_INFO', userInfo)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  setUserMsgChange({ commit }) {
    return new Promise((resolve, reject) => {
      commit('SET_USER_MSG_CHANGE', new Date().getTime())
    })
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_PERMISSIONS', [])
        commit('SET_USER_INFO', {})
        removeToken()
        resetRouter()
        dispatch('tagsView/delAllViews', null, { root: true })
        resolve()
      }).catch(error => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_PERMISSIONS', [])
        commit('SET_USER_INFO', {})
        removeToken()
        resetRouter()
        dispatch('tagsView/delAllViews', null, { root: true })
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      commit('SET_PERMISSIONS', [])
      commit('SET_USER_INFO', {})
      removeToken()
      resetRouter()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
